import Vue from 'vue'
import Router from 'vue-router'
import iView from 'iview'

import Util from '@/libs/util'
import store from '@/store'

import BasicLayout from '../BasicLayout'

Vue.use(Router)

// const Settings = () => import(/** settings */ '@/pages/Settings')

export const router = new Router({
  mode: 'history',
  base: '/adm/',
  fallback: true,
  routes: [
    {
      path: '/',
      component: BasicLayout,
      children: [
        {
          path: '', redirect: 'articles'
        },
        {
          path: 'account',
          component: () => import('@/pages/Account')
        },
        {
          path: 'password/reset',
          component: () => import('@/pages/password/Edit'),
          meta: {
            hideMenu: true
          }
        },
        {
          path: 'password/edit',
          component: () => import('@/pages/password/Edit'),
          meta: {
            hideMenu: true
          }
        },
        {
          path: 'settings',
          component: () => import(/* webpackChunkName: "setting" */ '@/pages/Settings')
        },
        /** Member */
        {
          path: 'members',
          component: () => import(/* webpackChunkName: "member" */'@/pages/staff/List')
        },
        {
          name: 'edit-member',
          path: 'members/:id/edit',
          component: () => import(/* webpackChunkName: "member" */'@/pages/staff/Edit')
        },
        /** Module Question */
        {
          path: 'questions',
          component: () => import(/* webpackChunkName: "question" */ '@/pages/question/List')
        },
        {
          path: 'questions/categories',
          component: () => import(/* webpackChunkName: "question" */'@/pages/question/Category')
        },
        {
          name: 'question-detail',
          path: 'questions/:id',
          component: () => import(/* webpackChunkName: "question" */'@/pages/question/Detail')
        },
        {
          path: 'questions/:id/answers/add',
          component: () => import(/* webpackChunkName: "question" */'@/pages/question/answer/Add')
        },
        {
          name: 'answer-edit',
          path: 'questions/:qid/answers/:aid/edit',
          component: () => import(/* webpackChunkName: "question" */'@/pages/question/answer/Edit')
        },
        // {
        //   path: 'categories',
        //   component: () => import(/* webpackChunkName: "question" */'@/pages/category/List')
        // },
        /** Module Article */
        {
          path: 'articles',
          component: () => import(/* webpackChunkName: "article" */'@/pages/article/List')
        },
        {
          path: 'articles/add',
          component: () => import(/* webpackChunkName: "article" */'@/pages/article/Add')
        },
        {
          path: 'articles/add-link',
          component: () => import(/* webpackChunkName: "article" */'@/pages/article/AddLink')
        },
        {
          path: 'articles/add-download',
          component: () => import(/* webpackChunkName: "article" */'@/pages/article/AddDownload')
        },
        {
          name: 'edit-article',
          path: 'articles/:id/edit',
          component: () => import(/* webpackChunkName: "article" */'@/pages/article/Edit')
        },
        {
          name: 'edit-article-link',
          path: 'articles/:id/edit-link',
          component: () => import(/* webpackChunkName: "article" */'@/pages/article/EditLink')
        },
        {
          name: 'edit-article-download',
          path: 'articles/:id/edit-download',
          component: () => import(/* webpackChunkName: "article" */'@/pages/article/EditDownload')
        },
        {
          path: 'articles/categories',
          component: () => import(/* webpackChunkName: "article" */'@/pages/article/Category')
        },
        /** Module Media */
        {
          path: 'media',
          meta: {
            allows: ['standard', 'pro']
          },
          component: () => import(/* webpackChunkName: "media" */'@/pages/media/List')
        },
        {
          path: 'media/add',
          meta: {
            allows: ['standard', 'pro']
          },
          component: () => import(/* webpackChunkName: "media" */'@/pages/media/Add')
        },
        {
          name: 'edit-media',
          path: 'media/:id/edit',
          meta: {
            allows: ['standard', 'pro']
          },
          component: () => import(/* webpackChunkName: "media" */'@/pages/media/Edit')
        },
        {
          name: 'media-unit-list',
          path: 'media/:id/unit',
          meta: {
            allows: ['standard', 'pro']
          },
          component: () => import(/* webpackChunkName: "media" */'@/pages/media/unit/List')
        },
        {
          name: 'add-media-unit',
          path: 'media/:id/unit/add',
          meta: {
            allows: ['standard', 'pro']
          },
          component: () => import(/* webpackChunkName: "media" */'@/pages/media/unit/Add')
        },
        {
          name: 'edit-media-unit',
          path: 'media/:id/unit/:delta/edit',
          meta: {
            allows: ['standard', 'pro']
          },
          component: () => import(/* webpackChunkName: "media" */'@/pages/media/unit/Edit')
        },
        {
          path: 'media/categories',
          meta: {
            allows: ['standard', 'pro']
          },
          component: () => import(/* webpackChunkName: "media" */'@/pages/media/Category')
        },
        /** Module Slideshow */
        {
          path: 'slideshows',
          component: () => import(/* webpackChunkName: "slideshow" */'@/pages/slideshow/List')
        },
        {
          path: 'slideshows/add',
          component: () => import(/* webpackChunkName: "slideshow" */'@/pages/slideshow/Add')
        },
        {
          name: 'edit-slideshow',
          path: 'slideshow/:id/edit',
          component: () => import(/* webpackChunkName: "slideshow" */'@/pages/slideshow/Edit')
        },
        /** Module Page */
        {
          path: 'pages',
          component: () => import(/* webpackChunkName: "media" */'@/pages/page/List')
        },
        {
          path: 'pages/add',
          component: () => import(/* webpackChunkName: "media" */'@/pages/page/Add')
        },
        {
          path: 'page/:id/edit',
          component: () => import(/* webpackChunkName: "media" */'@/pages/page/Edit')
        },
        {
          path: 'menus',
          component: () => import('@/pages/menu/List')
        }
      ]
    },
    {
      name: 'login',
      path: '/login',
      component: () => import(/* webpackChunkName: "user" */ '@/pages/Login'),
      beforeEnter: (to, from, next) => {
        // console.log('before enter login')
        if (store.state.app.logged_in) {
          next('/settings')
        } else {
          next()
        }
      }
    },
    {
      name: 'register',
      path: '/register',
      component: () => import(/* webpackChunkName: "user" */ '@/pages/Register'),
      beforeEnter: (to, from, next) => {
        if (store.state.app.logged_in) {
          next('/settings')
        } else {
          next()
        }
      }
    },
    {
      path: '/password/forgot',
      component: () => import(/* webpackChunkName: "user" */'@/pages/password/Forgot'),
      beforeEnter: (to, from, next) => {
        store.state.app.logged_in ? next('/settings') : next()
      }
    }
  ]
})

router.beforeEach((to, from, next) => {
  // console.log('router beforeEach', to)
  const isDependent = ['/login', '/register', '/password/forgot'].includes(to.path)
  let appType = (store.state.app.info && store.state.app.info.type) || 'basic'

  if (!store.state.app.logged_in && !isDependent) {
    // anonymous visiting protected pages.
    next('/login')
  } else if (
    store.state.app.ready &&
    to.meta.allows &&
    !to.meta.allows.includes(appType)) {
    // logged user visiting prohibited pages.
    next('/')
  } else {
    // access check pass
    iView.LoadingBar.start()
    Util.title(to.meta.title)
    next()
  }
})

router.beforeResolve((to, from, next) => {
  let appType = (store.state.app.info && store.state.app.info.type) || 'basic'
  if (to.meta.allows && !to.meta.allows.includes(appType)) {
    // console.log('before resolve interceptor')
    next('/')
    // iView.LoadingBar.finish()
  } else {
    next()
  }
})

router.afterEach((to, from, next) => {
  iView.LoadingBar.finish()
  window.scrollTo(0, 0)
})

export default router
